package com.hspedu.newData.LCR;

/**
 * @Author: yh
 * @description:
 * @CreateTime: 2025-05-28
 * @Version: 1.0
 */

public class LCR105岛屿的最大面积 {

    private int[][] dir = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

    int max = 0;
    int count = 0;

    public int maxAreaOfIsland(int[][] grid) {
        int x = grid.length;
        int y = grid[0].length;

        boolean[][] visited = new boolean[x][y];

        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                if (grid[i][j] == 1) {
                    dfs(grid, i, j);
                    max = Math.max(max, count);
                    count = 0;
                }
            }
        }
        return max;
    }

    public void dfs(int[][] grid,int x, int y) {

        if (x < 0 || y < 0 || x >= grid.length || y >= grid[0].length || grid[x][y] != 1) {
            return;
        }
        count++;
        grid[x][y] = 0;
        for (int i = 0; i < 4; i++) {
            int nextX = x + dir[i][0];
            int nextY = y + dir[i][1];
            dfs(grid, nextX, nextY);
        }
    }
}

